package main

import (
	_ "bugManagementPlatform/dataInit"
	"bugManagementPlatform/pojo"
	"bugManagementPlatform/utils"
	"bugManagementPlatform/variable"
	"github.com/fsnotify/fsnotify"
	"io/ioutil"
	"log"
	"strings"
	"time"
)

func RecursiveFolder(path string, watcher *fsnotify.Watcher)  {

	err := watcher.Add(path)
	if err != nil {
		log.Fatal(err)
	}

	files, _ := ioutil.ReadDir(path)
	for _, f := range files {
		if f.IsDir() {
			RecursiveFolder(path + "/" + f.Name(), watcher)
		}

	}
}

func main() {

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	//var count int8 = 0
	go func() {
		for {
			select {

			case event := <-watcher.Events:
				//log.Println("event:", event)
				//if event.Op & fsnotify.Write == fsnotify.Write {
				//	log.Println("modified file:", event.Name)
				//}

				log.Println("Name：", event.Name)
				log.Println("Op：", event.Op)

				//filrFullPath := strings.Split(event.Name, ".")

				//if event.Op.String() == "CREATE"{
				//	fmt.Println("创建文件夹")
				//	RecursiveFolder("event.Name", watcher)
				//}

				if event.Op.String() == "CREATE" && strings.HasSuffix(event.Name, ".html"){

					time.Sleep(time.Second * 1)

					var tr = pojo.TestReport{
						Id:    new(utils.Worker).GetId(),
						FileName:  event.Name,
					}
					variable.Fdb.Create(&tr)

					//count ++

					//if count%2 == 0{
					//	var tr = pojo.TestReport{
					//		Id:    new(utils.Worker).GetId(),
					//		FileName:  event.Name,
					//	}
					//	variable.Fdb.Create(&tr)
					//}
				}

				//nginx()

			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	RecursiveFolder("D:\\jmeterTest\\report", watcher)

	//err = watcher.Add("/path/to/file2") //也可以监听文件夹
	//if err != nil {
	//	log.Fatal(err)
	//}

	<-done

}

//func nginx() {
//	cmd := exec.Command("/usr/local/bin/lunchy", "restart", "nginx") //重启命令根据自己的需要自行调整
//	cmd.Run()
//}






